import { Injectable } from '@angular/core';
import { HttpClient, HttpHeaders } from '@angular/common/http';
import { Observable, throwError } from 'rxjs';
import { AppSettings } from '../../shared/config/app-settings';
import { map, catchError } from 'rxjs/operators';
import { UserService } from '../../shared/services/user.service';
import * as _ from 'lodash';
@Injectable({
  providedIn: 'root'
})
export class LoginService {
  myHttpHead = { headers: new HttpHeaders({ 'Content-Type': 'application/json' }) };
  httpOptions: any = {
    headers: new HttpHeaders({ 'Content-Type': 'application/json;charset=UTF-8' }),
    observe: 'response',
    responseType: 'text',
    withCredentials: true
  };
  constructor(private httpClient: HttpClient, private userService: UserService) { }
  /**
   * 
   * @param role 
   */
  isAuthenticated(roles: String[]): Observable<boolean> {
    return this.userService.getCurrentUser().pipe(
      map(user => {
        console.log(user);
        if (user) {
          return _.includes(roles, user.role);
          // return user.role === role;
        } else {
          return false;
        }
      }),
      catchError((err) => { return throwError(err) })
    )
  }

}
